package com.yygo.webapp.controller.material;

import com.yygo.model.Material;
import com.yygo.model.MaterialAudit;
import com.yygo.service.MaterialAuditManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by tiansha on 2015/10/31.
 */
@Controller
@RequestMapping("/auditMaterials")
public class AuditMaterialsController extends BaseMaterialController {

    @Autowired
    private MaterialAuditManager materialAuditManager;

    @RequestMapping(method = RequestMethod.GET)
    public Model handleRequest(@RequestParam(required = true, value = "maId") Long maId, HttpServletRequest request)
            throws Exception {
        MaterialAudit materialAudit = materialAuditManager.get(maId);
        List<Material> materials = materialAudit.getMaterialList();
        List<Material> completeMaterials = buildCompleteMaterials(materialAudit);
        mergeMaterials(materials, completeMaterials);
        materialAudit.setMaterialList(completeMaterials);

        Model model = new ExtendedModelMap();
        model.addAttribute(materialAudit);
        return model;
    }

}
